package com.funtonow.jqdk.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.funtonow.jqdk.R;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.ToastUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.FEEDBACK;

/**
 * @author Jiajien
 * @date 2019/6/25
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.et_feedback)
    EditText etFeedBack;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.iv_sub)
    ImageView ivSub;


    @Override
    public int getLayoutRes() {
        return R.layout.activity_feedback;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        initToolbar(getResources().getString(R.string.complaint));
        etFeedBack.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    ivSub.setBackgroundResource(R.drawable.icon_sub_select);
                    ivSub.setClickable(true);
                    tvNum.setText(String.format(Locale.getDefault(), "%d/200", s.length()));
                }
            }
        });
    }


    @OnClick({R.id.iv_sub})
    @Override
    public void onClick(View v) {
        feedback();
    }


    /**
     * 保存银行卡
     */
    public void feedback() {
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        Map<String, String> param = new HashMap<>();
        assert userInfoBean != null;
        param.put("appid", Constants.OWN_APP_ID);
        param.put("token", userInfoBean.getToken());
        param.put("openid", userInfoBean.getOpenid());
        param.put("content", etFeedBack.getText().toString());
        HttpUtil.getInstance().postDataAsynToNet(FEEDBACK, param, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                ToastUtils.show("提交成功");
                finish();
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

}
